package db_main

import (
	"errors"
	"fmt"
	"longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/db/mysql"
	"longmen/server/pkg/db/mysql/models/models_main"

	"gorm.io/gorm"
)

func GetActivityById(id int) (*models_main.Activity, error) {
	o := &models_main.Activity{}
	err := mysql.GetRTable(&models_main.Activity{}).
		Where("id = ?", id).
		Order("id asc").Limit(1).
		Find(o).
		Error
	if errors.Is(err, gorm.ErrRecordNotFound) {
		err = nil
	}
	if err != nil {
		return nil, fmt.Errorf("%s%s", util.Convert2(), err.Error())
	}
	return o, nil
}

func GetActivityListByParam(param map[string]interface{}, order string, page, size int) ([]*models_main.Activity, error) {
	o := make([]*models_main.Activity, 0)
	err := mysql.Condition(mysql.GetRTable(&models_main.Activity{}), param, "", order, page, size).
		Find(&o).
		Error
	return o, err
}
